/*
 * This page prepares the student for the manage students jsp
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.CourseDao;
import dao.EmployeeDao;
import dao.StudentDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Course;
import models.Employee;
import models.Role;
import models.Student;
import models.User;

/**
 *
 * @author Achie
 */
public class StudentController extends HttpServlet {

    private HibernateUtil hu = new HibernateUtil();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession ses = request.getSession();

        if (request.getParameter("searched") != null) { //if there was searched for a student
            if (ses.getAttribute("userrole").equals("Administrator")) {
                String search = request.getParameter("search");
                String searchType = request.getParameter("searchtype");

                if (searchType.equals("name")) {
                    searchStudentByName(request.getParameter("search"), request, response);
                } else if (searchType.equals("email")) {
                    searchStudentByEmail(request.getParameter("search"), request, response);
                }
            }
        } else {
            //Setup dao's              
            StudentDao sd = new StudentDao(hu.openSession());
            CourseDao cd = new CourseDao(hu.openSession());
            EmployeeDao ed = new EmployeeDao(hu.openSession());

            List courseid = new ArrayList();

            if (ses.getAttribute("userrole").equals("Teacher")) {//if it is a teacher                
                User user = (User) ses.getAttribute("user");//get userdata    
                List<Course> courseList = cd.findAllByTeacher(user.getMember_id());//get all teacher courses                
                List<Student> courseStudents = new ArrayList<Student>();

                for (int i = 0; i < courseList.size(); i++) { //for every course
                    Set<Student> student = courseList.get(i).getStudent();
                    List<Object> list = new ArrayList<Object>(student);
                    for (int j = 0; j < list.size(); j++) {
                        Student s = (Student) list.get(j);
                        courseStudents.add(s); //add the student
                    }
                }

                // Breadcrumbs
                Breadcrumbs[] breadcrumbs = {
                    new Breadcrumbs("Index", "Home"),
                    new Breadcrumbs("Student", "Manage student progress"),};
                request.setAttribute("breadcrumbs", breadcrumbs);

                request.setAttribute("students", courseStudents);
                redirect(request, response);

            } else { //if it is an administrator
                List<Student> students = sd.findAll(); //load all students

                // Breadcrumbs
                Breadcrumbs[] breadcrumbs = {
                    new Breadcrumbs("Index", "Home"),
                    new Breadcrumbs("Student", "Manage student progress"),};
                request.setAttribute("breadcrumbs", breadcrumbs);

                request.setAttribute("students", students);
                redirect(request, response);
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /*
     * this method redirects the page to manage students
     * it also prepares neccessary attributes as the courselist
     */
    public void redirect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CourseDao cd = new CourseDao(hu.openSession());
        HttpSession ses = request.getSession();
        User user = (User) ses.getAttribute("user"); 
        List<Course> courseList = cd.findAllByTeacher(user.getMember_id()); //find all courses by the teacher id
        request.setAttribute("courses", courseList); //set courselist in attribute

        if (ses.getAttribute("userrole").equals("Administrator")) { //if it's an administrator, load all courses
            List<Course> courseListx = cd.findAll();
            request.setAttribute("courses", courseListx); //set courselist in attribute
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/manage_students.jsp");
        dispatcher.forward(request, response);
    }

    /*
     * this method gives the result of the search (by name) for a student (by redirecting)  
     */
    public void searchStudentByName(String name, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao sd = new StudentDao(hu.openSession());
        List<Student> students = sd.findStudentByName(name);
        request.setAttribute("students", students);
        redirect(request, response);
    }
    
    /*
     * this method gives the result of the search (by e-mail) for a student (by redirecting)  
     */
    public void searchStudentByEmail(String email, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao sd = new StudentDao(hu.openSession());
        List<Student> students = sd.findStudentByEmail(email);
        request.setAttribute("students", students);
        redirect(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
